package com.bi.design.config;

import com.bi.design.consts.TimeConsts;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.format.DateTimeFormatter;

@Configuration
public class JacksonConfig {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            // Long型精度丢失问题
            builder.serializerByType(Long.TYPE, ToStringSerializer.instance);
            builder.serializerByType(Long.class, ToStringSerializer.instance);
            // 时间格式化
            builder.simpleDateFormat(TimeConsts.DATE_FORMATTER_TIME);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(TimeConsts.DATE_FORMATTER_DATE)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(TimeConsts.DATE_FORMATTER_TIME)));
            builder.deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(TimeConsts.DATE_FORMATTER_DATE)));
            builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(TimeConsts.DATE_FORMATTER_TIME)));
        };
    }
}
